package com.soccer.soccerclub.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soccer.soccerclub.entity.Commodity;
import com.soccer.soccerclub.entity.GameOrder;
import com.soccer.soccerclub.entity.SoccerGame;
import com.soccer.soccerclub.entity.User;
import com.soccer.soccerclub.entity.dto.GameOrderDto;
import com.soccer.soccerclub.service.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: soccer-club
 * @description:
 * @created: 2021/04/22 17:32
 */
@RestController
@RequestMapping("/admin")
@AllArgsConstructor
public class AdminController {
    private UserService userService;
    private OrderItemService orderItemService;
    private GameOrderService gameOrderService;
    private SoccerGameService soccerGameService;
    private CommodityService commodityService;

    /**
     * 管理员给用户充值
     * @param userId
     * @param money
     * @return
     */
    @PostMapping("/addMoneyForUser")
    public R addMoneyForUser(Integer userId, BigDecimal money){
        User user = userService.getById(userId);
        user.setMoney(user.getMoney().add(money));
        boolean update = userService.updateById(user);
        return R.ok(update);
    }

    /**
     * 管理员获取订单列表
     * @param page
     * @param id
     * @return
     */
    @GetMapping("/getAllOrderList")
    public R getAllOrderList(Page<GameOrder> page,String id){
        Page orderPage = null;
        if (StringUtils.isEmpty(id)){
            orderPage = gameOrderService.page(page);
        }else {
            orderPage = gameOrderService.page(page, new LambdaQueryWrapper<GameOrder>().like(GameOrder::getId, id));
        }
        List<GameOrderDto> gameOrderDtos = new ArrayList<>();
        List<GameOrder> orders = orderPage.getRecords();
        for (GameOrder order : orders) {
            if (order.getCommodityId()==null){//如果是球票订单
                GameOrderDto gameOrderDto = new GameOrderDto();
                SoccerGame soccerGame = soccerGameService.getById(order.getGameId());
                gameOrderDto.setGameName(soccerGame.getGameName());
                gameOrderDto.setOrderType(order.getOrderType());
//            gameOrderDto.setCommodityName();
                gameOrderDto.setDiscount(order.getDiscount());
                gameOrderDto.setActualPrice(order.getActualPrice());
                gameOrderDto.setPayablePrice(order.getPayablePrice());
                gameOrderDto.setCreatTime(order.getCreatTime());
                User user = userService.getById(order.getUserId());
                gameOrderDto.setNickName(user.getNickName());
                gameOrderDto.setStatus(order.getStatus());
                gameOrderDto.setPayTime(order.getPayTime());
                gameOrderDto.setTotal(order.getTotal());
                gameOrderDto.setId(order.getId());
                gameOrderDtos.add(gameOrderDto);
            }else if (order.getCommodityId()!=null){
                GameOrderDto gameOrderDto = new GameOrderDto();
                Commodity commodity = commodityService.getById(order.getCommodityId());
                gameOrderDto.setCommodityName(commodity.getName());
                gameOrderDto.setDiscount(order.getDiscount());
                gameOrderDto.setActualPrice(order.getActualPrice());
                gameOrderDto.setPayablePrice(order.getPayablePrice());
                gameOrderDto.setCreatTime(order.getCreatTime());
                User user = userService.getById(order.getUserId());
                gameOrderDto.setNickName(user.getNickName());
                gameOrderDto.setStatus(order.getStatus());
                gameOrderDto.setPayTime(order.getPayTime());
                gameOrderDto.setTotal(order.getTotal());
                gameOrderDto.setId(order.getId());
                gameOrderDtos.add(gameOrderDto);
            }
        }
        orderPage.setRecords(gameOrderDtos);
        return R.ok(orderPage);
    }
}
